package com.interest.business.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hy.corecode.idgen.WFGIdGenerator;
import com.interest.business.domain.InterestInterest;
import com.interest.business.domain.InterestRedeemCode;
import com.interest.business.domain.PayRecord;
import com.interest.business.domain.vo.OrderQueryVo;
import com.interest.business.domain.vo.OrderVo;
import com.interest.business.mapper.InterestInterestMapper;
import com.interest.business.mapper.InterestRedeemCodeMapper;
import com.interest.business.mapper.PayRecordMapper;
import com.interest.business.outer.request.OrderCreateBean;
import com.interest.business.outer.request.OrderRefundBean;
import com.interest.common.utils.DateUtils;
import com.interest.common.utils.StringUtils;
import com.interest.common.utils.uuid.IdUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.interest.business.mapper.InterestOrderMapper;
import com.interest.business.domain.InterestOrder;
import com.interest.business.service.IInterestOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-11
 */
@Service
public class InterestOrderServiceImpl implements IInterestOrderService {
    @Autowired
    private InterestOrderMapper interestOrderMapper;

    @Autowired
    WFGIdGenerator wfgIdGenerator;

    @Autowired
    InterestRedeemCodeMapper interestRedeemCodeMapper;

    @Autowired
    private InterestInterestMapper interestInterestMapper;

    @Autowired
    PayRecordMapper payRecordMapper;

    /**
     * 查询订单记录
     *
     * @param id 订单记录主键
     * @return 订单记录
     */
    @Override
    public InterestOrder selectInterestOrderById(String id) {
        return interestOrderMapper.selectInterestOrderById(id);
    }

    /**
     * 查询订单记录列表
     *
     * @param interestOrder 订单记录
     * @return 订单记录
     */
    @Override
    public List<InterestOrder> selectInterestOrderList(OrderQueryVo interestOrder) {
        return interestOrderMapper.selectInterestOrderList(interestOrder);
    }

    /**
     * 记录订单信息
     *
     * @param orderCreateBean
     * @return
     */
    @Override
    public int createOrder(OrderCreateBean orderCreateBean) {
        InterestOrder interestOrder = new InterestOrder();
        BeanUtils.copyProperties(orderCreateBean, interestOrder);
        interestOrder.setId(IdUtils.simpleUUID());
        interestOrder.setCreateTime(DateUtils.getNowDate());
        interestOrder.setOrderType("1");
        interestOrder.setOrderTime(DateUtils.parseDate(orderCreateBean.getOrderTime()));
        return interestOrderMapper.insertInterestOrder(interestOrder);
    }

    /**
     * 订单退款
     *
     * @param orderRefundBean
     * @return
     */
    @Override
    public int refundOrder(OrderRefundBean orderRefundBean) {
        InterestOrder interestOrder = new InterestOrder();
        BeanUtils.copyProperties(orderRefundBean, interestOrder);
        interestOrder.setId(IdUtils.simpleUUID());
        interestOrder.setCreateTime(DateUtils.getNowDate());
        interestOrder.setOrderType("2");
        interestOrder.setOrderTime(DateUtils.parseDate(orderRefundBean.getRefundTime()));
        return interestOrderMapper.insertInterestOrder(interestOrder);
    }


    /**
     * 记录订单信息
     *
     * @param orderCreateBean
     * @return
     */
    @Override
    @Transactional
    public int createOrderV2(OrderCreateBean orderCreateBean) {
        InterestOrder interestOrder = new InterestOrder();
        BeanUtils.copyProperties(orderCreateBean, interestOrder);
        String orderNo = "" + wfgIdGenerator.next();
        interestOrder.setId(IdUtils.simpleUUID());
        interestOrder.setOrderNo(orderNo);
        interestOrder.setCreateTime(DateUtils.getNowDate());
        interestOrder.setOrderType("1");
        interestOrder.setOrderTime(DateUtils.parseDate(orderCreateBean.getOrderTime()));
        //分配券码
        String goodsNo = interestOrder.getCustomerGoodsNo();
        //查询一个未分配的兑换码对象
        InterestRedeemCode code = interestRedeemCodeMapper.selectOneNotGrantCode(goodsNo);
        //查询指定兑换码
//        InterestRedeemCode code = interestRedeemCodeMapper.selectCodeByCode(goodsNo);

        if (code != null) {
            InterestInterest interest = interestInterestMapper.selectInterestInterestById(code.getInterestId());
            Date startTime = getStartAndEndTime(interest).get("startTime"),
                    endTime = getStartAndEndTime(interest).get("endTime");
            code.setOrderNo(orderNo);
            code.setSaleStatus("1"); //修改为已售
            code.setStatus("1");//修改为已分配
            code.setStartTime(startTime);
            code.setPayCount(code.getPayCount() + 1);
            code.setEndTime(endTime);
            interestRedeemCodeMapper.updateInterestRedeemCode(code);

            //下单记录表
            PayRecord payRecord = new PayRecord();
            payRecord.setId(IdUtils.simpleUUID());
            payRecord.setCode(code.getCode());
            payRecord.setInterestId(interest.getId());
            payRecord.setOrderNo(interestOrder.getOrderNo());
            payRecord.setSaleStatus(code.getSaleStatus());
            payRecord.setStatus(code.getStatus());
            payRecord.setStartTime(code.getStartTime());
            payRecord.setEndTime(code.getEndTime());
            payRecord.setOrderTime(interestOrder.getOrderTime());
            payRecordMapper.insertPayRecord(payRecord);
        }
        return interestOrderMapper.insertInterestOrder(interestOrder);
    }

    /**
     * 订单退款
     *
     * @param orderRefundBean
     * @return
     */
    @Override
    @Transactional
    public int refundOrderV2(OrderRefundBean orderRefundBean) {
        InterestOrder interestOrder = new InterestOrder();
        BeanUtils.copyProperties(orderRefundBean, interestOrder);
        interestOrder.setId(IdUtils.simpleUUID());
        interestOrder.setOrderNo("" + wfgIdGenerator.next());
        interestOrder.setCreateTime(DateUtils.getNowDate());
        interestOrder.setOrderType("2");
        interestOrder.setRefundTime(DateUtils.parseDate(orderRefundBean.getRefundTime()));

        //退款时券码状态重置
        String jtcOrderNo = orderRefundBean.getJtcOrderNo();
        OrderVo order = interestOrderMapper.selectInterestOrderByJtcOrderNo(jtcOrderNo);
        if(order!=null){
            interestRedeemCodeMapper.releaseCouponToPoolByOrderNo(order.getOrderNo());
        }
        return interestOrderMapper.insertInterestOrder(interestOrder);
    }

    private Map<String, Date> getStartAndEndTime(InterestInterest interestInterest) {
        Map<String, Date> data = new HashMap<>(2);
        Date startTime = DateUtils.getNowDate();
        data.put("startTime", startTime);
        data.put("endTime", DateUtils.parseDate("2099-12-31"));
        if (StringUtils.equals(interestInterest.getValidType(), "0")) {
            data.put("endTime", DateUtils.parseDate(interestInterest.getValidValue()));
        } else if (StringUtils.equals(interestInterest.getValidType(), "1")) {
            data.put("endTime", DateUtils.addDays(startTime, Integer.parseInt(interestInterest.getValidValue())));
        }
        return data;
    }
}
